﻿#include "callbackctrli.h"
#include <QDebug>
#include <QJsonParseError>
#include <QJsonObject>
#include <QCoreApplication>
#include <QThread>
#include <QJsonArray>

CallBackCtrlI::CallBackCtrlI(QObject *parent) : QObject(parent)
{
    m_pRobotWoker = new RobotWoker();
    connect(this,SIGNAL(sig_tts_play(QString)),m_pRobotWoker,SLOT(slot_tts_play(QString)));
    connect(this,SIGNAL(sig_tts_params(QString,QString,int,int)),m_pRobotWoker,SLOT(slot_tts_params(QString,QString,int,int)));
    connect(this,SIGNAL(sig_tts_play_file(QString)),m_pRobotWoker,SLOT(slot_tts_play_file(QString)));
    connect(this,SIGNAL(sig_tts_volume(int)),m_pRobotWoker,SLOT(slot_tts_volume(int)));
    connect(this,SIGNAL(sig_tts_Playing()),m_pRobotWoker,SLOT(slot_tts_Playing()));
    connect(this,SIGNAL(sig_tts_stop()),m_pRobotWoker,SLOT(slot_tts_stop()));

    connect(this,SIGNAL(sig_media_play(QString)),m_pRobotWoker,SLOT(slot_media_play(QString)));
    connect(this,SIGNAL(sig_media_Playing()),m_pRobotWoker,SLOT(slot_media_Playing()));
    connect(this,SIGNAL(sig_media_stop()),m_pRobotWoker,SLOT(slot_media_stop()));
    connect(this,SIGNAL(sig_media_volume(int)),m_pRobotWoker,SLOT(slot_media_volume(int)));

    connect(this,SIGNAL(sig_asr_build(QString)),m_pRobotWoker,SLOT(slot_asr_build(QString)));
    connect(this,SIGNAL(sig_asr_gramma(QString)),m_pRobotWoker,SLOT(slot_asr_gramma(QString)));
    connect(this,SIGNAL(sig_asr_start(int,bool)),m_pRobotWoker,SLOT(slot_asr_start(int,bool)));
    connect(this,SIGNAL(sig_asr_stop()),m_pRobotWoker,SLOT(slot_asr_stop()));

    connect(this,SIGNAL(sig_head_enable(bool,bool)),m_pRobotWoker,SLOT(slot_head_enable(bool,bool)));
    connect(this,SIGNAL(sig_hand_r_enable(bool,bool)),m_pRobotWoker,SLOT(slot_hand_r_enable(bool,bool)));
    connect(this,SIGNAL(sig_hand_l_enable(bool,bool)),m_pRobotWoker,SLOT(slot_hand_l_enable(bool,bool)));

    connect(this,SIGNAL(sig_hand_l_expand_init()),m_pRobotWoker,SLOT(slot_hand_l_expand_init()));
    connect(this,SIGNAL(sig_hand_l_swing_init()),m_pRobotWoker,SLOT(slot_hand_l_swing_init()));
    connect(this,SIGNAL(sig_hand_r_expand_init()),m_pRobotWoker,SLOT(slot_hand_r_expand_init()));
    connect(this,SIGNAL(sig_hand_r_swing_init()),m_pRobotWoker,SLOT(slot_hand_r_swing_init()));
    connect(this,SIGNAL(sig_head_lr_init()),m_pRobotWoker,SLOT(slot_head_lr_init()));
    connect(this,SIGNAL(sig_head_ud_init()),m_pRobotWoker,SLOT(slot_head_ud_init()));

    connect(this,SIGNAL(sig_script_play_content(QString)),m_pRobotWoker,SLOT(slot_script_play_content(QString)));
    connect(this,SIGNAL(sig_script_play_path(QString)),m_pRobotWoker,SLOT(slot_script_play_path(QString)));
    connect(this,SIGNAL(sig_script_play_stop()),m_pRobotWoker,SLOT(slot_script_play_stop()));
    connect(this,SIGNAL(sig_script_playing()),m_pRobotWoker,SLOT(slot_script_playing()));

    connect(this,SIGNAL(sig_json_auto_play()),m_pRobotWoker,SLOT(slot_json_auto_play()));
    connect(this,SIGNAL(sig_json_class_name(QString)),m_pRobotWoker,SLOT(slot_json_class_name(QString)));
    connect(this,SIGNAL(sig_json_playing()),m_pRobotWoker,SLOT(slot_json_playing()));
    connect(this,SIGNAL(sig_json_key_name(QString)),m_pRobotWoker,SLOT(slot_json_key_name(QString)));
    connect(this,SIGNAL(sig_json_list()),m_pRobotWoker,SLOT(slot_json_list()));
    connect(this,SIGNAL(sig_json_load(QString)),m_pRobotWoker,SLOT(slot_json_load(QString)));
    connect(this,SIGNAL(sig_json_stop()),m_pRobotWoker,SLOT(slot_json_stop()));

    connect(this,SIGNAL(sig_led_body(int,bool,bool,bool)),m_pRobotWoker,SLOT(slot_led_body(int,bool,bool,bool)));
    connect(this,SIGNAL(sig_led_head_ear(int,bool,bool,bool)),m_pRobotWoker,SLOT(slot_led_head_ear(int,bool,bool,bool)));
    connect(this,SIGNAL(sig_led_head_eye(int,int)),m_pRobotWoker,SLOT(slot_led_head_eye(int,int)));
    connect(this,SIGNAL(sig_led_head_eye_2(int,bool,bool,bool)),m_pRobotWoker,SLOT(slot_led_head_eye_2(int,bool,bool,bool)));
    connect(this,SIGNAL(sig_led_touch_set(int,int,int)),m_pRobotWoker,SLOT(slot_led_touch_set(int,int,int)));

    connect(this,SIGNAL(sig_set_avoid(bool)),m_pRobotWoker,SLOT(slot_set_avoid(bool)));
    connect(this,SIGNAL(sig_get_avoid()),m_pRobotWoker,SLOT(slot_get_avoid()));

    connect(this,SIGNAL(sig_set_system_cmd(QString)),m_pRobotWoker,SLOT(slot_set_system_cmd(QString)));
    connect(this,SIGNAL(sig_set_system_volume(int)),m_pRobotWoker,SLOT(slot_set_system_volume(int)));
    connect(this,SIGNAL(sig_get_system_volume()),m_pRobotWoker,SLOT(slot_get_system_volume()));

    connect(this,SIGNAL(sig_head_lr(int,int)),m_pRobotWoker,SLOT(slot_head_lr(int,int)));
    connect(this,SIGNAL(sig_head_ud(int,int)),m_pRobotWoker,SLOT(slot_head_ud(int,int)));
    connect(this,SIGNAL(sig_hand_l_expand(int,int)),m_pRobotWoker,SLOT(slot_hand_l_expand(int,int)));
    connect(this,SIGNAL(sig_hand_r_expand(int,int)),m_pRobotWoker,SLOT(slot_hand_r_expand(int,int)));
    connect(this,SIGNAL(sig_hand_l_swing(int,int)),m_pRobotWoker,SLOT(slot_hand_l_swing(int,int)));
    connect(this,SIGNAL(sig_hand_r_swing(int,int)),m_pRobotWoker,SLOT(slot_hand_r_swing(int,int)));

}

QString CallBackCtrlI::parseJson(QJsonObject jsObj)
{
    // process cmd
    QString strCmd = jsObj.value("type").toString();
    QJsonValue jsVal = jsObj.value("data");
    if("audio"==strCmd){
        QJsonObject jsObj = jsVal.toObject();
        if(jsObj.contains("tts")){
            QString strTTS = jsObj.value("tts").toString();
            emit sig_tts_play(strTTS);
        }
        else if(jsObj.contains("tts_params")){
            QString strTTS = jsObj.value("tts_params").toArray().at(0).toString();
            QString strSpeaker = jsObj.value("tts_params").toArray().at(1).toString();
            int nSpeed = jsObj.value("tts_params").toArray().at(2).toInt();
            int nPitch = jsObj.value("tts_params").toArray().at(3).toInt();
            emit sig_tts_params(strTTS,strSpeaker,nSpeed,nPitch);
        }
        else if(jsObj.contains("tts_file_path")){
            QString strFilePath = jsObj.value("tts_file_path").toString();
            emit sig_tts_play_file(strFilePath);

        }
        else if(jsObj.contains("tts_volume")){
            int nVolume = jsObj.value("tts_volume").toInt();
            emit sig_tts_volume(nVolume);
        }
        else if(jsObj.contains("tts_stop")){
            emit sig_tts_stop();
        }
        else if(jsObj.contains("tts_Playing")){
            emit sig_tts_Playing();


        }
        else if(jsObj.contains("media_play")){
            QString strFilePath = jsObj.value("media_play").toString();
            emit sig_media_play(strFilePath);
        }
        else if(jsObj.contains("media_volume")){
            int nVolume = jsObj.value("media_volume").toInt();
            emit sig_media_volume(nVolume);
        }
        else if(jsObj.contains("media_stop")){
            emit sig_media_stop();
        }
        else if(jsObj.contains("media_Playing")){
            emit sig_media_Playing();
        }
        else if(jsObj.contains("asr_build")){
            QString strBNF = jsObj.value("asr_build").toString();
            emit sig_asr_build(strBNF);
        }
        else if(jsObj.contains("asr_gramma")){
            QString strName = jsObj.value("asr_gramma").toString();
            emit sig_asr_gramma(strName);
        }
        else if(jsObj.contains("asr_start")){
            int nMaxDelayMs = jsObj.value("asr_start").toArray().at(0).toInt();
            bool bCmdMode = jsObj.value("asr_start").toArray().at(1).toBool();
            emit sig_asr_start(nMaxDelayMs,bCmdMode);
        }
        else if(jsObj.contains("asr_stop")){
            emit sig_asr_stop();
        }
    }
    else if("motor_enable"==strCmd){
        QJsonObject jsObj = jsVal.toObject();
        if(jsObj.contains("head_enable")){
            bool bHeadLR = jsObj.value("head_enable").toArray().at(0).toBool();
            bool bHeadUD = jsObj.value("head_enable").toArray().at(1).toBool();
            emit sig_head_enable(bHeadLR,bHeadUD);
        }
        else if(jsObj.contains("hand_r_enable")){
            bool bHandRExpand = jsObj.value("hand_r_enable").toArray().at(0).toBool();
            bool bHandRSwing= jsObj.value("hand_r_enable").toArray().at(1).toBool();
            emit sig_hand_r_enable(bHandRExpand,bHandRSwing);
        }
        else if(jsObj.contains("hand_l_enable")){
            bool bHandLExpand = jsObj.value("hand_l_enable").toArray().at(0).toBool();
            bool bHandLSwing= jsObj.value("hand_l_enable").toArray().at(1).toBool();
            emit sig_hand_l_enable(bHandLExpand,bHandLSwing);
        }
    }
    else if("motor_init"==strCmd){
        QJsonObject jsObj = jsVal.toObject();
        if(jsObj.contains("head_lr_init")){
            emit sig_head_lr_init();
        }
        else if(jsObj.contains("head_ud_init")){
            emit sig_head_ud_init();
        }
        else if(jsObj.contains("hand_r_expand_init")){
            emit sig_hand_r_expand_init();
        }
        else if(jsObj.contains("hand_l_expand_init")){
            emit sig_hand_l_expand_init();
        }
        else if(jsObj.contains("hand_r_swing_init")){
            emit sig_hand_r_swing_init();
        }
        else if(jsObj.contains("hand_l_swing_init")){
            emit sig_hand_l_swing_init();
        }
    }
    else if("motor"==strCmd){
        QJsonObject jsObj = jsVal.toObject();
        if(jsObj.contains("head_lr")){
            int nHeadLRAngel = jsObj.value("head_lr").toArray().at(0).toInt();
            int nHeadLRSpeed = jsObj.value("head_lr").toArray().at(1).toInt();
            emit sig_head_lr(nHeadLRAngel,nHeadLRSpeed);

        }
        else if(jsObj.contains("head_ud")){
            int nHeadUDAngel = jsObj.value("head_ud").toArray().at(0).toInt();
            int nHeadUDSpeed = jsObj.value("head_ud").toArray().at(1).toInt();
            emit sig_head_ud(nHeadUDAngel,nHeadUDSpeed);

        }
        else if(jsObj.contains("hand_r_expand")){
            int nHandRExpandAngel = jsObj.value("hand_r_expand").toArray().at(0).toInt();
            int nHandRExpandSpeed = jsObj.value("hand_r_expand").toArray().at(1).toInt();
            emit sig_hand_r_expand(nHandRExpandAngel,nHandRExpandSpeed);

        }
        else if(jsObj.contains("hand_l_expand")){
            int nHandLExpandAngel = jsObj.value("hand_l_expand").toArray().at(0).toInt();
            int nHandLExpandSpeed = jsObj.value("hand_l_expand").toArray().at(1).toInt();
            emit sig_hand_l_expand(nHandLExpandAngel,nHandLExpandSpeed);

        }
        else if(jsObj.contains("hand_r_swing")){
            int nHandRSwingAngel = jsObj.value("hand_r_swing").toArray().at(0).toInt();
            int nHandRSwingSpeed = jsObj.value("hand_r_swing").toArray().at(1).toInt();
            emit sig_hand_r_swing(nHandRSwingAngel,nHandRSwingSpeed);

        }
        else if(jsObj.contains("hand_l_swing")){
            int nHandLSwingAngel = jsObj.value("hand_l_swing").toArray().at(0).toInt();
            int nHandLSwingSpeed = jsObj.value("hand_l_swing").toArray().at(1).toInt();
            emit sig_hand_l_swing(nHandLSwingAngel,nHandLSwingSpeed);

        }
        else if(jsObj.contains("wheel_run")){
            int nWheelDistance = jsObj.value("wheel_run").toArray().at(0).toInt();
            int nWheelSpeed = jsObj.value("wheel_run").toArray().at(1).toInt();
            emit sig_wheel_run(nWheelDistance,nWheelSpeed);
        }
        else if(jsObj.contains("wheel_rotate")){
            int nWheelAngel = jsObj.value("wheel_rotate").toArray().at(0).toInt();
            int nWheelSpeed = jsObj.value("wheel_rotate").toArray().at(1).toInt();
            emit sig_wheel_rotate(nWheelAngel,nWheelSpeed);
        }
        else if(jsObj.contains("wheel_run_diff")){
            bool bLeft = jsObj.value("wheel_run_diff").toArray().at(0).toBool();
            int nLeftSpeed = jsObj.value("wheel_run_diff").toArray().at(1).toInt();
            bool bRight = jsObj.value("wheel_run_diff").toArray().at(2).toBool();
            int nRightSpeed = jsObj.value("wheel_run_diff").toArray().at(3).toInt();
            emit sig_wheel_run_diff(bLeft,nLeftSpeed,bRight,nRightSpeed);
        }
        else if(jsObj.contains("wheel_stop")){
            bool bStop = jsObj.value("wheel_stop").toBool();
            emit sig_wheel_stop(bStop);
        }

    }
    else if("script"==strCmd){
        QJsonObject jsObj = jsVal.toObject();
        if(jsObj.contains("play_path")){
            QString strFilePath = jsObj.value("play_path").toString();
            emit sig_script_play_path(strFilePath);
        }
        else if(jsObj.contains("play_content")){
            QString strContent = jsObj.value("play_content").toString();
            emit sig_script_play_content(strContent);

        }
        else if(jsObj.contains("play_stop")){
            emit sig_script_play_stop();
        }
        else if(jsObj.contains("script_playing")){
            emit sig_script_playing();
        }

    }
    else if("json"==strCmd){
        QJsonObject jsObj = jsVal.toObject();
        if(jsObj.contains("load")){
            QString strFilePath = jsObj.value("load").toString();
            emit sig_json_load(strFilePath);
        }
        else if(jsObj.contains("key_name")){
            QString strKeyName = jsObj.value("key_name").toString();
            emit sig_json_key_name(strKeyName);
        }
        else if(jsObj.contains("class_name")){
            QString strClassName = jsObj.value("class_name").toString();
            emit sig_json_class_name(strClassName);
        }

        else if(jsObj.contains("stop")){
            emit sig_json_stop();
        }
        else if(jsObj.contains("auto_play")){
            emit sig_json_auto_play();
        }
        else if(jsObj.contains("json_playing")){
            emit sig_json_playing();
        }
        else if(jsObj.contains("json_list")){
            emit sig_json_list();
        }
    }
    else if("led"==strCmd){
        QJsonObject jsObj = jsVal.toObject();
        if(jsObj.contains("head_eye")){
            int nIndexF = jsObj.value("head_eye").toArray().at(0).toInt();
            int nIndexB = jsObj.value("head_eye").toArray().at(1).toInt();
            emit sig_led_head_eye(nIndexF,nIndexB);

        }
        else if(jsObj.contains("head_eye_2")){
            int nDelay = jsObj.value("head_eye_2").toArray().at(0).toInt();
            bool bR = jsObj.value("head_eye_2").toArray().at(1).toBool();
            bool bG = jsObj.value("head_eye_2").toArray().at(2).toBool();
            bool bB = jsObj.value("head_eye_2").toArray().at(3).toBool();
            emit sig_led_head_eye_2(nDelay,bR,bG,bB);

        }
        else if(jsObj.contains("head_ear")){
            int nDelay = jsObj.value("head_ear").toArray().at(0).toInt();
            bool bR = jsObj.value("head_ear").toArray().at(1).toBool();
            bool bG = jsObj.value("head_ear").toArray().at(2).toBool();
            bool bB = jsObj.value("head_ear").toArray().at(3).toBool();
            emit sig_led_head_ear(nDelay,bR,bG,bB);
        }
        else if(jsObj.contains("body")){
            int nDelay = jsObj.value("body").toArray().at(0).toInt();
            bool bR = jsObj.value("body").toArray().at(1).toBool();
            bool bG = jsObj.value("body").toArray().at(2).toBool();
            bool bB = jsObj.value("body").toArray().at(3).toBool();
            emit sig_led_body(nDelay,bR,bG,bB);
        }
        else if(jsObj.contains("touch_set")){
            int nMode = jsObj.value("touch_set").toArray().at(0).toInt();
            int nColor = jsObj.value("touch_set").toArray().at(1).toInt();
            int nFreq = jsObj.value("touch_set").toArray().at(2).toInt();
            emit sig_led_touch_set(nMode,nColor,nFreq);

        }
    }
    else if("avoid"==strCmd){
        QJsonObject jsObj = jsVal.toObject();
        if(jsObj.contains("set_avoid")){
            bool bAvoid = jsObj.value("set_avoid").toBool();
            emit sig_set_avoid(bAvoid);
        }
        else if(jsObj.contains("get_avoid")){
            emit sig_get_avoid();
        }
    }
    else if("config"==strCmd){
        QJsonObject jsObj = jsVal.toObject();
        if(jsObj.contains("set_system_volume")){
            int nVolume = jsObj.value("set_system_volume").toInt();
            emit sig_set_system_volume(nVolume);
        }
        else if(jsObj.contains("get_system_volume")){
            emit sig_get_system_volume();
        }
        else if(jsObj.contains("set_system_cmd")){
            QString strCmd = jsObj.value("set_system_cmd").toString();
            emit sig_set_system_cmd(strCmd);
        }
    }
    QString str = QString("{\"type\":\"audio\",\"data\":\"ok\"}");
    //m_pMainWindow->writeTextEdit(str);
    return str;
}

std::string CallBackCtrlI::recvMsg(const std::string &str, const Ice::Current &)
{
    qDebug()<<"str:"<<QString::fromUtf8(str.c_str());
    //QByteArray bOneCmd(str.c_str(), str.length());
    QByteArray bOneCmd = QString::fromUtf8(str.c_str()).toUtf8();
    QJsonParseError jsError;
    QJsonDocument jsDoc = QJsonDocument::fromJson(bOneCmd, &jsError);
    if( jsError.error != QJsonParseError::NoError ){
        qDebug()<<jsError.error;
        return "";
    }
    QJsonObject jsObj = jsDoc.object();
    if( !jsObj.contains("type") ){
        qDebug()<<"no type";
        return "";
    }
    emit sig_write_text(QString::fromUtf8(str.c_str()));
    QString string = parseJson(jsObj);
    return string.toUtf8().toStdString();
}

